// index.js
// 获取应用实例
const app = getApp()

Page({
	data: {
		fieldName: "点击选择",
		questionsNum: 5,
		fieldList: [],
		fieldNames: [],
		fieldId: null
	},
	bindPickerChange(e) {
		const fidx = e.detail.value;
		this.setData({
			fieldName: this.data.fieldList[fidx]["field_type"],
			fieldId: this.data.fieldList[fidx]["id"]
		})
		console.log(this.data);
	},
	getFields() {
		wx.request({
			url: 'http://localhost:8000/fields',
			success: res => {
				this.setData({
					fieldList: res.data,
					fieldNames: res.data.map(field => field.field_type)
				})
			},
		})
	},
	changeNum(e){
		this.setData({
			questionsNum:e.detail.value
		})
	},
	goExam(){
		const { fieldId, questionsNum } = this.data;
		if(fieldId && questionsNum){
			wx.navigateTo({
				url: `../exam/exam?fieldId=${fieldId}&questionsNum=${questionsNum}`,
				success: function(res) {
					// 通过eventChannel向被打开页面传送数据
					console.log(res);
				},
			})
		}else if(questionsNum <= 0){
			wx.showModal({
				title: "提示",
				content: "请输入有效数字"
			})
		}else if(!fieldId){
			wx.showModal({
				title: "提示",
				content: "请选择题库"
			})
		}
	},
	
	goRecord(){
		wx.navigateTo({
			url: `../record/record`,
			success: function(res) {
				console.log(res);
			},
		})
	},

	onLoad: function () {
		console.log("index onLoad")
		this.getFields();
	}
})
